package cn.dckeji.gameBox.controller;


import cn.dckeji.gameBox.common.utils.MD5;
import cn.dckeji.gameBox.common.utils.ResultUtil;
import cn.dckeji.gameBox.dto.Result;
import cn.dckeji.gameBox.entity.Info;
import cn.dckeji.gameBox.service.InfoService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2018-08-20
 */
@RestController
@RequestMapping("/info")
public class InfoController {
    @Autowired
    private InfoService infoService;
    @GetMapping("getInfoList")
    public Map<String, Object> getInfoList(Integer page, Integer limit, @RequestParam(value = "gameName",required = false) String gameName, @RequestParam(value = "typeId",required = false) String typeId, @RequestParam(value = "date",required = false) String date){
        Page<Info> infoPage = new Page<>();
        infoPage.setSize(limit);
        infoPage.setCurrent(page);
        EntityWrapper<Info> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(gameName)) {
            entityWrapper.like("gameName",gameName);
        }
        if(StringUtils.isNotBlank(typeId)){
            entityWrapper.like("typeId",typeId);
        }
        if(StringUtils.isNotBlank(date)){
            String starttime = date.substring(0, 11);
            String enddate = date.substring(date.length() - 10);
            entityWrapper.between("crateTime",starttime+" 00:00:00",enddate+" 23:59:59");
        }
        entityWrapper.orderBy("crateTime",false);
        infoPage = infoService.selectPage(infoPage,entityWrapper);
        return ResultUtil.resultPage(infoPage.getTotal(),infoPage.getRecords());
    }
    /**
     * 保存游戏信息
     * @param info
     * @return
     */
    @PostMapping("saveInfo")
    public Result saveInfo(Info info) {
        if (info.getId() == null) {
            info.setCrateTime(new Date());
            info.setUploadTime(new Date());
            info.setState(1);
            infoService.insert(info);
        }else {
            info.setUpdateTime(new Date());
            infoService.updateById(info);
        }
        return ResultUtil.success();
    }
    /**
     * 修改状态
     * @param id
     * @return
     */
    @PostMapping("stateEdit")
    public Result stateEdit(Integer id,Integer state) {
        Info info=infoService.selectById(id);
        info.setState(state);
        info.setUpdateTime(new Date());
        if(state==1){
            info.setUploadTime(new Date());
        }
        infoService.updateById(info);
        return ResultUtil.success();
    }
    /**
     * 删除角色
     * @param id
     * @return
     */
    @PostMapping("deleteById")
    public Result deleteById(Integer id) {
        infoService.deleteById(id);
        return ResultUtil.success();
    }
}

